package com.hotdog.servlet;

import com.hotdog.mapper.UserMapper;
import com.hotdog.pojo.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Set;

@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String u = request.getParameter("username");
        String password = request.getParameter("password");
        String username = new String(u.getBytes(StandardCharsets.ISO_8859_1), "UTF-8");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);

        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();


        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserMapper mapper = sqlSession.getMapper(UserMapper.class);


         User users = mapper.selectAll(username,password);
        System.out.println(username);
        System.out.println(password);

        if(users!=null){
            request.getRequestDispatcher("/SuccessServlet").forward(request,response);
        }else {
            response.setStatus(302);
            response.setHeader("location","/FailureServlet");
        }



    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                 doGet(request,response);
    }
}
